<?php
namespace app\wenchuang\controller;

use app\wenchuang\model\InfoModel;
use app\wenchuang\validate\InfoValidate;
use cmf\controller\AdminBaseController;

class InfoController extends AdminBaseController
{
    public function index()
    {
        $name = input('name');
        $start = strtotime(input('start'));
        $end = strtotime(input('end'));
        $status = input('status');
        $info = InfoModel::order(['list_order'=>'desc','create_time'=>'desc'])
            ->where(function ($query) use ($name,$start,$end,$status){
                if($name){
                    $query->where('name','like','%'.$name.'%');
                }
                if($start&&!$end){
                    $query->where('create_time','>',$start);
                }
                if($start&&$end){
                    $query->whereBetween('create_time',[$start,$end]);
                }
                if(!$start&&$end){
                    $query->where('create_time','<',$end);
                }
                if($status != ''){
                    $query->where('status','=',$status);
                }
            })
            ->paginate(10);
        $info->appends(input());
        return $this->fetch('index',[
            'info' => $info
        ]);
    }

    public function listOrder()
    {
        parent::listOrders(new InfoModel());
        $this->success('ok');
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function addPost()
    {
        $data=input('post.');
        $validate = new InfoValidate();
        $request = $validate->scene('add')->check($data);
        if ($request !== true){
            $this->error($validate->getError());
        }
        if(InfoModel::create($data)){
            $this->success('添加成功','Info/index');
        }else{
            $this->error('添加失败');
        }
    }

    public function edit()
    {
        $id = input('id');
        $info = InfoModel::where('id','=',$id)->find();
        return $this->fetch('edit',[
            'info' => $info
        ]);
    }

    public function editPost()
    {
        $data=input('post.');
        $info = InfoModel::where('id','=',$data['id'])->find()->toArray();
        $info_data = [
            'id' => $info['id'],
            'name' => $info['name'],
            'details' => $info['details'],
            'status' => $info['status']
        ];
        if(empty(array_diff($data,$info_data))&&empty(array_diff($info_data,$data))){
            $this->error('数据未修改');
        }
        $validate = new InfoValidate();
        $request = $validate->scene('edit')->check($data);
        if ($request !== true){
            $this->error($validate->getError());
        }
        if(InfoModel::update($data,['id'=>$data['id']])){
            $this->success('修改成功','Info/index');
        }else{
            $this->error('修改失败');
        }
    }

    public function deleteSoft()
    {
        $id = input('id');
        $info= InfoModel::where('id','=',$id)->find();
        $delete = $info->delete();
        if($delete){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        };
    }

    public function changeStatus()
    {
        $id = input('id');
        $info = InfoModel::find($id);
        if($info['status'] == 1){
            InfoModel::update(['status'=>0],['id'=>$id]);
        }else{
            InfoModel::update(['status'=>1],['id'=>$id]);
        }
        $this->success('修改成功');
    }
}